<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\helper\Time;
use app\common\service\OrderMaterial as OrderMaterialService;

/**
	 * 定时同步时效统计
     *
	 * @date 2020年12月28日
	 */
class CronAging extends Command
{

	protected $logName = 'cronAging';

	protected function configure()
	{
		$this->setName('cronAging')
		->setDescription('定时同步时效统计');
	}

	protected function execute(Input $input, Output $output)
	{
		$startTime=date('Y-m-d h:i:s',time());
		try {
			db()->startTrans();
			$orderMaterialService = new OrderMaterialService();
			$where = [
				['create_time ', '>', (time()-86400*90)],
				['stock_type ', 'not in', [10,11]],
			];
			$orderInfo = db('order')->field('id,create_time,client_ordernum,caichuang_over_time,over_time,caichuang_can_do_time')->where($where)->select();

			foreach ($orderInfo as $k => $v) {
				$oRemark="";$oAbnormal=0;
				//$list = db('order_material')->where('order_id', $orderId)->where('material_id', '>', 0)->order($order)->field($field)->select();
				$omList = $orderMaterialService->getListByOrderId($v['id']);
				$checkclothTimeAll=$shrinkTimeAll=$arriveTimeAll=$caigouConfirmTimeAll=$caigouOverTimeAll=[0];
				$isCheckcloth=$isShrink=0;
				foreach ($omList as $ki => $vi) {
					$omRemark="";$omAbnormal=0;
					if ($vi['caigou_show'] == 0) {
    					unset($omList[$ki]); // 去除不在采购进度显示条目
    					continue;
    				}
					$omiList=db('order_material_item')->where('order_material_id',$vi['mid'])->select();
					foreach ($omiList as $komi => $vomi) {
						$checkclothTimeAll[]=$vomi['checkcloth_time'];//验布时间
						$shrinkTimeAll[]=$vomi['shrink_time'];//缩水时间
					}
					if($vi['arrive_time']==0){
						$atTime=0;
					}
					$arriveTimeAll[]=$vi['arrive_time'];//到料时间
					$caigouConfirmTimeAll[]=$vi['caigou_confirm_time'];//打标时间
					$caigouOverTimeAll[]=$vi['caigou_over_time'];//批色通过时间
					$materialStatus = $orderMaterialService->getMaterialStatus($vi['mid']);

					if($materialStatus['is_checkcloth']!=0) $isCheckcloth=1;
					if($materialStatus['is_shrink']!=0) $isShrink=1;
					$mcheckclothTime=max($checkclothTimeAll);//最后验布时间
					$mshrinkTime=max($shrinkTimeAll);//最后缩水时间
					//有效打标时效
					$cgConfirmSecond=0;
					if($vi['arrive_time']!=0 && $vi['caigou_confirm_time']!=0){
						if($vi['arrive_time']>$vi['caigou_confirm_time']){
							$omRemark.="到料时间大于打标时间 / ";
							$omAbnormal=$omAbnormal+1;

						}else{
							$effMatArriveTimeStr=calTime($vi['arrive_time'])[0];
							$effMatCgConfirmTimeEnd=calTime($vi['caigou_confirm_time'])[1];
							$cgConfirmSunSecond=checkSunday($effMatArriveTimeStr,$effMatCgConfirmTimeEnd);//时间段内周日数
							$cgConfirmSecond=$effMatCgConfirmTimeEnd-$effMatArriveTimeStr-$cgConfirmSunSecond;
						}
					}

					//有效批色时效
					$CgOverSecond=0;
					if($vi['arrive_time']!=0 && $vi['caigou_over_time']!=0 ){
						if($vi['arrive_time']>$vi['caigou_over_time']) {
							$omRemark.="到料时间大于批色时间 / ";
							$omAbnormal=$omAbnormal+1;

						}else{
							$effmCorArriveTimeStr=calTime($vi['arrive_time'])[0];
							$effmCorCgOverTimeEnd=calTime($vi['caigou_over_time'])[1];
							$CgOverSunSecond=checkSunday($effmCorArriveTimeStr,$effmCorCgOverTimeEnd);//时间段内周日数
							$CgOverSecond=$effmCorCgOverTimeEnd-$effmCorArriveTimeStr-$CgOverSunSecond;
						}


					}

					//有效验布时效
					$mcheckcSecond=0;
					if($mcheckclothTime!=0 && $vi['arrive_time']!=0){
						if($vi['arrive_time']>$mcheckclothTime) {
							$omRemark.="到料时间大于验布时间 / ";
							$omAbnormal=$omAbnormal+1;

						}else{
							$effmCheckcTimeStr=calTime($vi['arrive_time'])[0];
							$effmCheckcTimeEnd=calTime($mcheckclothTime)[1];
							$mcheckcSunSecond=checkSunday($effmCheckcTimeStr,$effmCheckcTimeEnd);//时间段内周日数
							$mcheckcSecond=$effmCheckcTimeEnd-$effmCheckcTimeStr-$mcheckcSunSecond;
						}

					}

					//有效缩水时效
					$mShrinkSecond=0;
					if($mshrinkTime!=0 && ($mcheckclothTime!=0 || $vi['arrive_time']!=0)){
						if($vi['arrive_time']>$mshrinkTime) {
							$omRemark.="到料时间大于缩水时间 / ";
							$omAbnormal=$omAbnormal+1;
						}else{
							$effmShrinkTimeStr=($materialStatus['is_checkcloth']!=0)?calTime($mcheckclothTime)[0]:calTime($vi['arrive_time'])[0];//区分需要验布和不需要验布
							$efmfShrinkTimeEnd=calTime($mshrinkTime)[1];
							$mShrinkSunSecond=checkSunday($effmShrinkTimeStr,$efmfShrinkTimeEnd);//时间段内周日数
							$mShrinkSecond=$efmfShrinkTimeEnd-$effmShrinkTimeStr-$mShrinkSunSecond;
						}

					}
					$agingMaterial=[
						'order_id'=>$v['id'],
						'client_ordernum'=>$v['client_ordernum'],
						'order_material_id'=>$vi['mid'],
						'material_id'=>$vi['material_id'],
						'material_type'=>$vi['mInfo']['type'],
						'is_checkcloth'=>$vi['need_checkcloth'],
						'is_shrink'=>$vi['need_shrink'],
						'arrive_time'=>$vi['arrive_time'],
						'make_tag_time'=>$vi['caigou_confirm_time'],
						'approval_color_time'=>$vi['caigou_over_time'],
						'checkcloth_time'=>$mcheckclothTime,
						'shrink_time'=>$mshrinkTime,
						'day_make_tag'=>ceil($cgConfirmSecond/86400),
						'time_make_tag'=>ceil($cgConfirmSecond/86400*24),
						'time_approval_color'=>ceil($CgOverSecond/86400*24),
						'time_checkcloth'=>ceil($mcheckcSecond/86400*24),
						'time_shrink'=>ceil($mShrinkSecond/86400*24),
						'create_time'=>time(),
						'update_time'=>time(),
						'remark'=>$omRemark,
						'abnormal'=>$omAbnormal
					];
					db('aging_material')->insert($agingMaterial, true);
				}
				$arriveTime= isset($atTime)?$atTime:max($arriveTimeAll);if(isset($atTime)) unset($atTime);
				$caigouConfirmTime=max($caigouConfirmTimeAll);//最后打标时间
				$caigouOverTime=max($caigouOverTimeAll);//最后批色时间
				$checkclothTime=max($checkclothTimeAll);//最后验布时间
				$shrinkTime=max($shrinkTimeAll);//最后缩水时间
				$caichuangCanDoTime=$v['caichuang_can_do_time'];

				//有效采购时效
				$caigouSecond=0;
				if($v['create_time']!=0 && $arriveTime!=0 && $arriveTime > $v['create_time'] ){
//				$effCreateTime=calCreateTime($v['create_time']);//有效下单时间
//				$effArriveTimeEnd=calTime($arriveTime)[1]; //有效到料时间
					//$caigouSunSecond=checkSunday($v['create_time'],$arriveTime);//时间段内周日数
					$caigouSecond=$arriveTime-$v['create_time'];
				}
				//有效批色时效
				$apColorSecond=0;
				if($caigouOverTime!=0 && $arriveTime!=0 && $caigouOverTime > $arriveTime){
//				$effArriveTimeStr=calTime($arriveTime)[0];
//				$effCaigouOverTimeEnd=calTime($caigouOverTime)[1];
					//$apColorSunSecond=checkSunday($arriveTime,$caigouOverTime);
					$apColorSecond=$caigouOverTime-$arriveTime;
				}

				//有效验布时效
				$checkclothSecond=0;
				if($checkclothTime!=0 && $arriveTime!=0 && $checkclothTime > $arriveTime){
//					$effCaigouConfirmTimeStr=calTime($caigouConfirmTime)[0];
//					$effCheckclothTimeEnd=calTime($checkclothTime)[1];
					//$checkclothSunSecond=checkSunday($arriveTime,$checkclothTime);
					$checkclothSecond=$checkclothTime-$arriveTime;
				}
				//有效缩水时效
				$ShrinkSecond=0;
				if($shrinkTime!=0 && ($checkclothTime!=0 || $arriveTime!=0)){
//					$effShrinkTimeStr=($isCheckcloth==1)?calTime($checkclothTime)[0]:calTime($arriveTime)[0];//区分需要验布和不需要验布
//					$effShrinkTimeEnd=calTime($shrinkTime)[1];
					//$ShrinkSunSecond=($isCheckcloth==1)?checkSunday($checkclothTime,$shrinkTime):checkSunday($arriveTime,$shrinkTime);
					$arriveOrCheck=($isCheckcloth!=0) ? $checkclothTime : $arriveTime;
					$ShrinkSecond=$shrinkTime-$arriveOrCheck;
				}
				//有效裁前时效
				$caiBeforeSecond=0;
				if($caichuangCanDoTime!=0 && $v['create_time']!=0 && $caichuangCanDoTime > $v['create_time']){
//					$effCreateTime=calCreateTime($v['create_time']);//区分需要验布和不需要验布
//					$effCaichuangCanDoTimeEnd=calTime($caichuangCanDoTime)[1];
					//$caiBeforeSunSecond=checkSunday($v['create_time'],$caichuangCanDoTime);
					$caiBeforeSecond=$caichuangCanDoTime-$v['create_time'];
				}

				//有效裁床时效
				$caichuangSecond=0;
				if($v['create_time']!=0 && $v['caichuang_over_time']!=0 && $v['caichuang_over_time'] > $caichuangCanDoTime){
//					$effCaichuangCanDoTimeStr=calTime($caichuangCanDoTime)[0];
//					$effOverTimeTimeEnd=calTime($v['caichuang_over_time'])[1];
					//$caichuangSunSecond=checkSunday($caichuangCanDoTime,$v['caichuang_over_time']);
					$caichuangSecond=$v['caichuang_over_time']-$caichuangCanDoTime;
				}

				//有效缝前时效
				$beforeSecond=0;
				if($caichuangCanDoTime!=0 && $v['caichuang_over_time']!=0 && $v['caichuang_over_time'] > $v['create_time']){
//					$effBeforeCreateTime=calCreateTime($v['create_time']);//有效下单时间
//					$effBeforeOverTimeEnd=calTime($v['caichuang_over_time'])[1];
					//$beforeSunSecond=checkSunday($caichuangCanDoTime,$v['caichuang_over_time']);
					$beforeSecond=$v['caichuang_over_time']-$v['create_time'];
				}

				$agingOrderInfo=[
					'order_id'=>$v['id'],
					'client_ordernum'=>$v['client_ordernum'],
					'order_time'=>$v['create_time'],
					'arrive_time'=>$arriveTime,
					'make_tag_time'=>$caigouConfirmTime,
					'approval_color_time'=>$caigouOverTime,
					'checkcloth_time'=>$checkclothTime,
					'shrink_time'=>$shrinkTime,
					'caichuang_can_do_time'=>$v['caichuang_can_do_time'],
					'caichuang_over_time'=>$v['caichuang_over_time'],
					'is_checkcloth'=>$isCheckcloth,
					'is_shrink'=>$isShrink,
					'day_caigou'=> ceil($caigouSecond/86400),
					'day_make_tag'=>0,
					'day_approval_color'=>ceil($apColorSecond/86400),
					'day_checkcloth'=>ceil($checkclothSecond/86400),
					'day_shrink'=>ceil($ShrinkSecond/86400),
					'day_caichuang_before'=>ceil($caiBeforeSecond/86400),
					'day_caichuang'=>ceil($caichuangSecond/86400),
					'day_chejian_before'=>ceil($beforeSecond/86400),
					'time_caigou'=> ceil($caigouSecond/86400*24),
					'time_make_tag'=>0,
					'time_approval_color'=>ceil($apColorSecond/86400*24),
					'time_checkcloth'=>ceil($checkclothSecond/86400*24),
					'time_shrink'=>ceil($ShrinkSecond/86400*24),
					'time_caichuang_before'=>ceil($caiBeforeSecond/86400*24),
					'time_chejian_before'=>ceil($beforeSecond/86400*24),
					'create_time'=>time(),
					'update_time'=>time(),
				];
				db('aging_order')->insert($agingOrderInfo, true);
			}

			db()->commit();
			$endTime=date('Y-m-d h:i:s',time());
			$this->_consoleLog('更新成功'.date('Y-m-d h:i:s',time()),'cronAging');
		} catch (\Exception $e) {
			db()->rollback();
			$msg = "错误:".$e->getMessage();
			$this->_consoleLog($msg, 'Exception');
			$output->write(json_encode(['code' => 0, 'msg' => $msg, 'data' => ['start'=>$startTime, 'end'=>$endTime, 'time' => date('Y-m-d h:i:s',time())]]));
		}
	}


	/**
	 * 记录日志并打印
	 *
	 * @author KING
	 * @date 2020年6月11日
	 *
	 * @param string $msg
	 * @param string $flag
	 * @param string $format
	 */
	private function _consoleLog($msg, $flag, $format=true) {
		consoleLog($msg, $flag, $format);
		addLog($msg, $this->logName);
	}

}
